package com.ctrip.cap.device.android.impl;

import java.util.Set;

import com.ctrip.cap.device.AbstractDeviceStore;
import com.ctrip.cap.device.DeviceStore;
import com.ctrip.cap.device.android.AndroidDevice;
import com.ctrip.cap.device.exceptions.AndroidDeviceException;
import com.ctrip.cap.device.exceptions.DeviceNotFoundException;

/**
 * 
 * @author ltyao
 *
 */
public class DefaultDeviceStore extends AbstractDeviceStore<AndroidDevice>
		implements DeviceStore<AndroidDevice> {

	DefaultDeviceManager deviceManager = new DefaultDeviceManager(true);

	static class StoreHolder {

		static final DefaultDeviceStore instance = init();

		static DefaultDeviceStore init() {
			DefaultDeviceStore instance;
			instance = new DefaultDeviceStore();
			instance.initAndroidDevices(false);
			return instance;
		}
	}

	public static DefaultDeviceStore getInstance() {
		return StoreHolder.instance;
	}

	/**
	 * call once
	 * 
	 * @param shouldKeepAdbAlive
	 */
	public void initAndroidDevices(boolean shouldKeepAdbAlive)
			throws AndroidDeviceException {

		deviceManager.initializeAdbConnection();
		Set<AndroidDevice> androidDevices = deviceManager.getAndroidDevices();

		if (androidDevices.isEmpty()) {
			throw new DeviceNotFoundException(
					"No android devices were found. Please start an Android hardware device via USB, "
							+ "or check other device offline problem such as open USB Debug");
		}
		devices.addAll(androidDevices);
		devicesInUse.addAll(androidDevices);

	}

	@Override
	public void shutdown() {
		deviceManager.shutdown();
	}

	@Override
	public void shutdownForcely() {
		deviceManager.shutdownForcely();

	}
}
